<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<% 
 /**
  功能名称:选择教师的模态窗口
  创建时间:2010-7-13
  创建人:
**/
%>
<%
String path = request.getContextPath();
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>用户选择</title>
	<link href="<%=path %>/css/index_css.css" rel="stylesheet" type="text/css" />
	<script src="<%=path %>/dwr/engine.js"></script>
	<script src="<%=path %>/dwr/util.js"></script>
	<script src="<%=path %>/dwr/interface/shiyueDwr.js"></script>
	<script language="javascript" src="<%=path %>/js/syAjax.js"></script>
	<script language="javascript" src="<c:url value='/js/json2.js'/>"></script>
	<script type="text/javascript" src="<%=path%>/js/jquery.js"></script>
	
	
	<style type="text/css">
		#selected{
			border:#919b9c 1px solid;background:#F8F8F8;padding:10px;min-height:30px;
		}
		#selected span{margin:0 5px 0 0;white-space:nowrap;float:left;width:100px;}
	</style>
	<script type="text/javascript">
	var paramArr=parent.giveParamArr();
	var selectType = "${flag}";
	if(selectType!='radio') {
		selectType='checkbox';
	}	

	
	function load(){
      shiyueDwr.findAllGradeByYear('${applicationScope.currentYear}',1,findGradeCallBack);
      shiyueDwr.findOrganizationBytype(1,findSubjectCallBack);
      shiyueDwr.findOrganizationBytype(2,findDeptCallBack);
      searchByUserNameLOAD();
    }
    function findGradeCallBack(orgMap) {
	     DWRUtil.addOptions("grade", orgMap);
	     if("${requestScope.gradeId}"!="")
	     {
	     	document.getElementById("grade").value="${requestScope.gradeId}";
	     }
    }
    function findSubjectCallBack(orgMap){	
	     DWRUtil.addOptions("subject", orgMap);
	     if("${requestScope.SELECTSUBJECTID}"!="")
	     {
	     	document.getElementById("subject").value="${requestScope.SELECTSUBJECTID}";
	     }
    }
    function findDeptCallBack(orgMap){
    	DWRUtil.addOptions("dept", orgMap);
    	if("${requestScope.SELECTDEPTID}"!="")
	     {
	     	document.getElementById("dept").value="${requestScope.SELECTDEPTID}";
	     }
    }
    
    function changeOrg(orgId){
    	if(orgId==""){
    		return;
    	}
    	//发送ajax请求，组织id，年度，获取该组织下的所有成员
		syAjax.request("post","<%=path %>/selectUserByOrgIdAndYearorganizationAction.action","organization.organizationId="+orgId+"&years=${applicationScope.currentYear}",{success:initSuccessResult,fail:onFail,type:'plain'});
		
    }
    function selectAll(chxName,chx){
    	var chxs=document.getElementsByName(chxName);
    	for(var i=0;i<chxs.length;i++){
    		var flag=chxs[i].checked!=chx.checked;
    		if(chxs[i].disabled){//如果是禁用的
    			continue;
    		}
    		chxs[i].checked=chx.checked;
    		if(flag){
    			jQuery(chxs[i]).triggerHandler("click");//执行用jQuery为checkbox绑定的click事件
    		}
    	}
    }
    
    function changeGrade(gradeId){
    	if(gradeId==""){
    		return;
    	}
    	//发送ajax请求，组织id，年度，获取该组织下的所有成员
		syAjax.request("post","<%=path %>/selectUserByGradeIdorganizationAction.action","grade.gradeId="+gradeId+"&years=${applicationScope.currentYear}&role.roleId=member",{success:initSuccessResult,fail:onFail,type:'plain'});
    	
    }
    
    
      function searchByUserNameLOAD(){
    	var nameStr = '';
    	var grade = '';
    	var subject = '';
    	var dept = '';
    	document.getElementById('divUser').innerHTML="正在加载用户列表，请稍后...";
    	//发送ajax请求，组织id，年度，获取该组织下的所有成员
		syAjax.request("post","<%=path %>/selectUserByNameorganizationAction.action","user.name="+nameStr+"&grade.gradeId="+grade+"&subjectId="+subject+"&deptId="+dept+"&user.userType=2",{success:initSuccessResultLOAD,fail:onFail,type:'plain'});
    }
    function initSuccessResultLOAD(response){
    	var data = JSON.parse(response);
    	var showStr="<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\" bgcolor=\"#97B1C8\"><tr>";
    	if(data!=null){
    		if(data.length>0){
		    	for(var i=0;i<data.length;i++){
		    		if(data.length==(i+1) && (i+1)%5!=0){
		    			showStr+="<td height=\"25\" align=\"left\" valign=\"middle\" bgcolor=\"#F8F8F8\" colspan='"+(5-(i+1)%5+1)+"'>";
		    		}else{
		    			showStr+="<td height=\"25\" align=\"left\" valign=\"middle\" bgcolor=\"#F8F8F8\">";
		    		}
		    		if(paramArr.indexOf(data[i].userId+",")!=-1){
		    			showStr+="<input type='"+selectType+"' name=\"chkUser\" checked=\"checked\" value=\""+data[i].userId+"_"+data[i].name+"\"";
		    			<c:if test="${requestScope.ROLEID eq 'sysadmin'}">
		    			if(data[i].account=="admin"){
		    			showStr+="disabled=\"disabled\"";
		    			}
		    			</c:if>
		    			 showStr+="/>";
		    			showStr+="<font color=\"red\">"+data[i].name+"</font>";
		    		}else{
		    			showStr+="<input type='"+selectType+"' name=\"chkUser\" value=\""+data[i].userId+"_"+data[i].name+"\"";
		    			<c:if test="${requestScope.ROLEID eq 'sysadmin'}">
		    			if(data[i].account=="admin"){
		    			showStr+="disabled=\"disabled\"";
		    			}
		    			</c:if>
		    			showStr+="/>";
		    			showStr+=data[i].name;
		    		}
		    		showStr+="</td>";
		    		if((i+1)%5==0){
		    			showStr+="</tr><tr>";
		    		}
		    	}
	    	}else{
	    		showStr+="<td colspan=\"5\" height=\"25\" align=\"center\" valign=\"middle\" bgcolor=\"#F8F8F8\">暂无数据</td>";
	    	}
	    	showStr+="</tr></table>";
	    	document.getElementById('divUser').innerHTML=showStr;
	    	bindClickEvent();
    	}
    	
    	
    }
    
    function searchByUserName(){
    	var nameStr = document.getElementById('UserName').value;
    	var grade = document.getElementById('grade').value;
    	var subject = document.getElementById('subject').value;
    	var dept = document.getElementById('dept').value;
    	document.getElementById('divUser').innerHTML="正在加载用户列表，请稍后...";
    	//发送ajax请求，组织id，年度，获取该组织下的所有成员
		syAjax.request("post","<%=path %>/selectUserByNameorganizationAction.action","user.name="+nameStr+"&grade.gradeId="+grade+"&subjectId="+subject+"&deptId="+dept+"&user.userType=2",{success:initSuccessResult,fail:onFail,type:'plain'});
    }
    function initSuccessResult(response){
    	var data = JSON.parse(response);
    	var showStr="<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\" bgcolor=\"#97B1C8\"><tr>";
    	if(data!=null){
    		if(data.length>0){
		    	for(var i=0;i<data.length;i++){
		    		if(data.length==(i+1) && (i+1)%5!=0){
		    			showStr+="<td height=\"25\" align=\"left\" valign=\"middle\" bgcolor=\"#F8F8F8\" colspan='"+(5-(i+1)%5+1)+"'>";
		    		}else{
		    			showStr+="<td height=\"25\" align=\"left\" valign=\"middle\" bgcolor=\"#F8F8F8\">";
		    		}
		    			showStr+="<input type='"+selectType+"' name=\"chkUser\" value=\""+data[i].userId+"_"+data[i].name+"\"";
		    			<c:if test="${requestScope.ROLEID eq 'sysadmin'}">
		    			if(data[i].account=="admin"){
		    			showStr+="disabled=\"disabled\"";
		    			}
		    			</c:if>
		    			showStr+="/>";
		    			showStr+=data[i].name;
		    		
		    		
		    		
		    		showStr+="</td>";
		    		if((i+1)%5==0){
		    			showStr+="</tr><tr>";
		    		}
		    	}
	    	}else{
	    		showStr+="<td colspan=\"5\" height=\"25\" align=\"center\" valign=\"middle\" bgcolor=\"#F8F8F8\">暂无数据</td>";
	    	}
	    	showStr+="</tr></table>";
	    	document.getElementById('divUser').innerHTML=showStr;
	    	bindClickEvent();
    	}
    	
    	
    }
    
    function onFail(){
		alert("网络繁忙，请稍后再试");
	}
	
	var returnStr="";
	function doReturnValue(){
		var gradeId = parent.orgId;//获取年级或组织ID
		<c:if test="${flag ne 'radio'}">var chkArr = document.getElementsByName('pid');</c:if>
		<c:if test="${flag eq 'radio'}">var chkArr = document.getElementsByName('chkUser');</c:if>
		if(chkArr!=null && chkArr.length>0){
			for(var i=0;i<chkArr.length;i++){
				if(chkArr[i].checked==true){
					returnStr+=chkArr[i].value+",";
				}
			}
			
		}
		// 2010-07-29 修改
		if('${requestScope.actionURL}'!="")
		{
		  syAjax.request("post","<%=path %>/${requestScope.actionURL}","selectGradeId=${SELECTGRADEID}&returnArr="+returnStr+"&gradeId="+gradeId+"&role.roleId=${requestScope.ROLEID}&years=${applicationScope.currentYear}",{success:initSuccessResult2,fail:onFail,type:'plain'});
		}else{
		  initSuccessResult2('');
		}
	}
	
	function initSuccessResult2(data){
		//window.returnValue=returnStr;
		//window.close();
		
		parent.doChooseUser(returnStr);
	}
	//重新绑定checkbox的事件
	function bindClickEvent(){
		$(":checkbox[name='chkUser'][checked]").each(function(){
    		createPid(this);
    	}); 
		$(":checkbox[name='chkUser']").click(function(){
			createPid(this);
    	});
    	$(":checkbox[name='pid']").each(function(){
			$(":checkbox[name='chkUser'][value='"+this.value+"']").attr("checked",true);//将已选中的同步到搜索出来的列表中
    	});
	}
	//创建一个checkbox到已选择列表
	function createPid(obj){
    		var chkUser=obj;
    		if(chkUser.checked){
    			var val=chkUser.value.substr(chkUser.value.indexOf("_")+1);//取名字
    			var checkbox=$("<input name='pid' type='checkbox' value='"+chkUser.value+"' checked='checked'/>");
    			checkbox.attr("disabled",chkUser.disabled);
    			checkbox.click(function(){//给刚添加checkbox添加点击事件
    				if(!this.checked){//如果是取消则同步
    					$(":checkbox[name='chkUser'][value='"+chkUser.value+"']").attr("checked",false);
    					//var font=$("~font",chkUser);//去掉font
    					//font.replaceWith(font.text());
    					$(this).parent().remove();
    				}
    			});
    			$("#selected").append($("<span></span>").append(checkbox).append(val));
    		}else{
    			$(":checkbox[name='pid'][value='"+chkUser.value+"']").parent().remove();
    		}
    };
    //页面加载
    jQuery(function($){
    	bindClickEvent();  	
    });
	</script>

  </head>
  
  <body onload="load();" bgcolor="#ECE9D8">
<table width="700" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td align="left" valign="top" bgcolor="#ECE9D8" class="border_2"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td align="left" valign="top" class="tb_10 border_2"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td height="30" align="left" valign="top" style="white-space: nowrap;">用户：
              <input id="UserName" name="UserName" type="text" size="15" /> 
              &nbsp;年级：              
              <select name="select" id="grade" onchange="searchByUserName();" >
              </select> 
              &nbsp;学科组：
              <select name="select2" id="subject" onchange="searchByUserName();">
                
              </select>
              &nbsp;部门：
              <select name="select3" id="dept" onchange="searchByUserName();">
                
              </select>
              &nbsp;
              <input type="button" onclick="searchByUserName();" name="Submit2" value="搜索" /></td>              
          </tr>
          <tr><td><c:if test="${flag!='radio'}"><input type="checkbox" onclick="selectAll('chkUser',this)"/><b>全选/取消</b></c:if></td></tr>
        </table>
        <div id="divUser">
        
          </div>
          </td>
      </tr>
    </table>
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td height="1" bgcolor="#AAA899"></td>
        </tr>
        <tr>
          <td height="1" bgcolor="#FFFFFF"></td>
        </tr>
        <c:if test="${flag ne 'radio'}"><%--如果是checkbox才显示--%>
         <tr>
        	<td>
        		<b>已选</b><a href="javascript:void(0);" onclick="jQuery('#selected :checkbox').attr('checked',false).click();" style="margin-left:5px;">从已选中清空</a>
        	</td>
        </tr>
        <tr>
        	<td id="selected">
        	</td>
        </tr>
        </c:if>
        <tr>
          <td height="35" align="center" valign="bottom"><input type="button" name="Submit" onclick="doReturnValue();" value="提 交" /></td>
        </tr>
      </table></td>
  </tr>
</table>
</body>
</html>
